/**
 * 
 */
package com.risetek.tft.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.risetek.tft.entity.TOperate;
import com.risetek.tft.entity.TOperateId;
import com.risetek.tft.entity.TRole;
import com.risetek.tft.service.MenuService;
import com.risetek.tft.service.RoleService;
import com.zlinepay.framework.bean.AjaxJson;
import com.zlinepay.framework.bean.DwzModel;

/**
 * @author 
 * 角色管理
 * 
 */
@Controller
@RequestMapping(value = "/role")
public class RoleAction extends BaseAction {

	@RequestMapping(value = "/add")
	public String add(  ModelMap model) {
		TRole dto = new TRole();
			model.addAttribute("dto", dto);
			model.addAttribute("menus", menuService.getAll());
		return "role/edit";
	}
	
	@RequestMapping(value = "/edit")
	public String edit(Integer id, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		if (id != null) {
			model.addAttribute("dto", roleService.findById(id));
		}
		model.addAttribute("menus", menuService.getAll());
		return "role/edit";
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public DwzModel list(DwzModel dataGrid,  HttpServletRequest request, ModelMap model)
			throws Exception {
		   StringBuffer hql = new StringBuffer("select new map(a.id as id,a.name as name,a.code as code,a.remark as remark) FROM TRole a ");
		if(StringUtils.isNotEmpty(dataGrid.getOrderField())) {
			hql.append( " order by a."+dataGrid.getOrderField()+"  "+dataGrid.getOrderDirection());
		}else{
			hql.append(" order by  a.id ");
		}
		String countHql = "select count(*) FROM TRole a";
	    dataGrid = this.roleService.pagedQuery(hql.toString(),countHql,   dataGrid);
	    return dataGrid;
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public AjaxJson save(String[] operates, @ModelAttribute("dto") TRole dto,
		  HttpServletRequest request,
			ModelMap model) {
		AjaxJson json = new AjaxJson();
		try {
			if(operates != null) {
				for(String opid : operates) {
					String[] tempIds = opid.split("-");
					TOperate op = new TOperate();
					op.setId(new TOperateId(Integer.valueOf(tempIds[0]), tempIds[1]));
					dto.getTOperates().add(op);
				}
			}
			roleService.saveOrUpdate(dto);
			json.setSuccess(true);
			json.setMsg("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("保存失败！");
		}
		return json;
	}

	@RequestMapping(value = "/remove")
	@ResponseBody
	public AjaxJson del(Integer[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		AjaxJson json = new AjaxJson();
		try {
			roleService.delete(ids);
			json.setSuccess(true);
			json.setMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("删除失败！");
		}
		return json;
	}

	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuService menuService;
	
}
